package com.example.teadoctor;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.Volley;

import java.util.ArrayList;

public class KnowFragment extends Fragment {

    ImageView imageView1,imageView2,imageView3,imageView4,imageView5,
            imageView6,imageView7,imageView8,imageView9,imageView10,
            imageView11,imageView12,imageView13,imageView14,imageView15,
            imageView16,imageView17,imageView18,imageView19,imageView20;
    TextView textView1,textView2,textView3,textView4,textView5,
            textView6,textView7,textView8,textView9,textView10,
            textView11,textView12,textView13,textView14,textView15,
            textView16,textView17,textView18,textView19,textView20;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.page_know,container,false);
        // 获取TeaDBHelper对象
        TeaDBHelper dbHelper = new TeaDBHelper(getActivity());

        // 获取SQLiteDatabase对象
        SQLiteDatabase db = dbHelper.getReadableDatabase();

        String query = "SELECT * FROM tea_info";
        Cursor cursor = db.rawQuery(query, null);
        ArrayList<String> dataList = new ArrayList<>();
        ArrayList<String> urlList = new ArrayList<>();
        ArrayList<String> nameList = new ArrayList<>();
        while(cursor.moveToNext()) {
            int id = cursor.getInt(cursor.getColumnIndex("id"));
            String name = cursor.getString(cursor.getColumnIndex("name"));
            String image = cursor.getString(cursor.getColumnIndex("image"));
            String type = cursor.getString(cursor.getColumnIndex("type"));
            String year = cursor.getString(cursor.getColumnIndex("year"));
            String description = cursor.getString(cursor.getColumnIndex("description"));
            String record = id + "   " + name+ "  image" + image + "  " + type + "  " + year + "  " + description;
            urlList.add(image);
            dataList.add(record);
            nameList.add(name);
        }

        imageView1 = view.findViewById(R.id.image1);
        imageView2 = view.findViewById(R.id.image2);
        imageView3 = view.findViewById(R.id.image3);
        imageView4 = view.findViewById(R.id.image4);
        imageView5 = view.findViewById(R.id.image5);
        imageView6 = view.findViewById(R.id.image6);
        imageView7 = view.findViewById(R.id.image7);
        imageView8 = view.findViewById(R.id.image8);
        imageView9 = view.findViewById(R.id.image9);
        imageView10 = view.findViewById(R.id.image10);
        imageView11 = view.findViewById(R.id.image11);
        imageView12 = view.findViewById(R.id.image12);
        imageView13 = view.findViewById(R.id.image13);
        imageView14 = view.findViewById(R.id.image14);
        imageView15 = view.findViewById(R.id.image15);
        imageView16 = view.findViewById(R.id.image16);
        imageView17 = view.findViewById(R.id.image17);
        imageView18 = view.findViewById(R.id.image18);
        imageView19 = view.findViewById(R.id.image19);
        imageView20 = view.findViewById(R.id.image20);
        textView1 = view.findViewById(R.id.text1);
        textView2 = view.findViewById(R.id.text2);
        textView3 = view.findViewById(R.id.text3);
        textView4 = view.findViewById(R.id.text4);
        textView5 = view.findViewById(R.id.text5);
        textView6 = view.findViewById(R.id.text6);
        textView7 = view.findViewById(R.id.text7);
        textView8 = view.findViewById(R.id.text8);
        textView9 = view.findViewById(R.id.text9);
        textView10 = view.findViewById(R.id.text10);
        textView11 = view.findViewById(R.id.text11);
        textView12 = view.findViewById(R.id.text12);
        textView13 = view.findViewById(R.id.text13);
        textView14 = view.findViewById(R.id.text14);
        textView15 = view.findViewById(R.id.text15);
        textView16 = view.findViewById(R.id.text16);
        textView17 = view.findViewById(R.id.text17);
        textView18 = view.findViewById(R.id.text18);
        textView19 = view.findViewById(R.id.text19);
        textView20 = view.findViewById(R.id.text20);
        for (int i = 0; i < urlList.size(); i++) {
            String url = urlList.get(i);
            final String name = nameList.get(i);
            final TextView textView;
            final ImageView imageView;
            switch (i) {
                case 0:
                    imageView = imageView1;
                    textView = textView1;
                    break;
                case 1:
                    imageView = imageView2;
                    textView = textView2;
                    break;
                case 2:
                    imageView = imageView3;
                    textView = textView3;
                    break;
                case 3:
                    imageView = imageView4;
                    textView = textView4;
                    break;
                case 4:
                    imageView = imageView5;
                    textView = textView5;
                    break;
                case 5:
                    imageView = imageView6;
                    textView = textView6;
                    break;
                case 6:
                    imageView = imageView7;
                    textView = textView7;
                    break;
                case 7:
                    imageView = imageView8;
                    textView = textView8;
                    break;
                case 8:
                    imageView = imageView9;
                    textView = textView9;
                    break;
                case 9:
                    imageView = imageView10;
                    textView = textView10;
                    break;
                case 10:
                    imageView = imageView11;
                    textView = textView11;
                    break;
                case 11:
                    imageView = imageView12;
                    textView = textView12;
                    break;
                case 12:
                    imageView = imageView13;
                    textView = textView13;
                    break;
                case 13:
                    imageView = imageView14;
                    textView = textView14;
                    break;
                case 14:
                    imageView = imageView15;
                    textView = textView15;
                    break;
                case 15:
                    imageView = imageView16;
                    textView = textView16;
                    break;
                case 16:
                    imageView = imageView17;
                    textView = textView17;
                    break;
                case 17:
                    imageView = imageView18;
                    textView = textView18;
                    break;
                case 18:
                    imageView = imageView19;
                    textView = textView19;
                    break;
                case 19:
                    imageView = imageView20;
                    textView = textView20;
                    break;
                default:
                    continue;
            }
            textView.setText(nameList.get(i));
            ImageRequest imageRequest = new ImageRequest(url, new Response.Listener<Bitmap>() {
                @Override
                public void onResponse(Bitmap bitmap) {
                    imageView.setImageBitmap(bitmap);
                    imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                    //Toast.makeText(getApplicationContext(), "图片加载成功", Toast.LENGTH_SHORT).show();
                }
            }, 0, 0, ImageView.ScaleType.CENTER_CROP, null, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    // 处理网络请求错误
                    imageView.setImageResource(R.mipmap.logo);
                }
            });
            RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
            requestQueue.add(imageRequest);

            //点击图片进行跳转页面
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 创建 Intent 对象，用于跳转到目标 Activity
                    Intent intent = new Intent(getContext(), Information.class);
                    intent.putExtra("name", name); // 将茶名作为参数传递给目标 Activity

                    startActivity(intent); // 启动跳转操作
                }
            });

        }



        /*ArrayAdapter<String> adapter = new ArrayAdapter<>(Objects.requireNonNull(getActivity()), android.R.layout.simple_list_item_1, dataList);
        ListView listView = view.findViewById(R.id.Pst_my_know);
        listView.setAdapter(adapter);*/
        // 关闭结果集和数据库连接
        cursor.close();
        db.close();

        return view;
    }

}

